package com.xyzh.application.config.security;


import com.xyzh.application.base.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.TokenGranter;
import org.springframework.security.oauth2.provider.token.*;

import javax.annotation.Resource;

/**
 * @Author JZB
 * @Description 服务授权认证
 * @Date 2021-06-01
 * @Param
 * @return
 **/
@Configuration
@EnableAuthorizationServer
public class AuthorizationServer extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;
    @Resource
    private TokenStore tokenStore;
    @Resource
    private UserServiceImpl userService;

    @Autowired
    private TokenGranter tokenGranter;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new myPasswordEncoder();
    }



    @Value("#{applicationProperies.authoration.clientId}")
    private String client_id;

    @Value("#{applicationProperies.authoration.clientSecret}")
    private String client_secret;

    /**
     * 客户端信息配置，可配置多个客户端，这里可以使用配置文件进行代替
     *
     * @param clients 客户端设置
     * @throws Exception 异常
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients)
            throws Exception {
        clients.inMemory()// 使用in-memory存储
                .withClient(client_id)// client_id
                .secret(passwordEncoder().encode(client_secret))//客户端密钥
                .authorizedGrantTypes("password", "refresh_token", "authorization_code","wechat")// 该client允许的授权类型authorization_code,password,refresh_token,implicit,client_credentials
                .scopes("all")
                .refreshTokenValiditySeconds(60 * 60 * 24 * 30)
                .accessTokenValiditySeconds(60 * 60 * 24 * 30)//60*60*24*
        ;
    }


    /**
     * 配置端点
     *
     * @param endpoints 端点
     * @throws Exception 异常
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                .tokenGranter(tokenGranter)
                .authenticationManager(authenticationManager)
                .userDetailsService(userService)
                .tokenStore(tokenStore)
        ;
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) {
        security.tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()")
                .allowFormAuthenticationForClients();
    }

}
